﻿using System;
using System.Web.Mvc;
using BookShop.BLL;
using BookShop.DTO;
using BookShop.Library.Constant;

namespace BookShop.Admin.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Login(string username, string password)
        {
            try
            {
                var mod = ModeratorService.Login(username, password);
                if (mod == null)
                {
                    ModelState.AddModelError("", "Thông tin đăng nhập không chính xác!");
                    return View();
                }
                Session.Add(SessionKey.Moderator, mod);
                return RedirectToAction("Index", "Home");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e);
                return View();
            }
        }

        public ActionResult LogOut()
        {
            if (Session[SessionKey.Moderator] != null)
            {
                Session.Remove(SessionKey.Moderator);
            }
            return RedirectToAction("LogIn", "Home");
        }
    }
}
